package model

import (
	"gorm.io/gorm"
)

type User struct {
	Nickname   string         `gorm:"type:varchar(30);"`
	Email      string         `gorm:"not null;unique"`
	Password   string         `gorm:"type:char(60);"`
	Mobile     *string        `gorm:"type:char(11);"`
	UserId     int64          `gorm:"primaryKey;AUTO_INCREMENT"`
	Created    int64          `gorm:"autoCreateTime:milli"`
	Updated    int64          `gorm:"autoUpdateTime:milli"`
	Deleted    gorm.DeletedAt `gorm:"index"`
	Accounts   []Account      `gorm:"foreignKey:UserId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	CashIns    []CashIn       `gorm:"foreignKey:UserId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	CashOuts   []CashOut      `gorm:"foreignKey:UserId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	OrderBuys  []OrderBuy     `gorm:"foreignKey:BuyerId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	OrderSales []OrderSale    `gorm:"foreignKey:SalerId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	DealBuys   []Deal         `gorm:"foreignKey:BuyerId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	DealSales  []Deal         `gorm:"foreignKey:SalerId;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`

	ProfilePhotoHash string
	ProfilePhoto     ProfilePhoto `gorm:"foreignKey:ProfilePhotoHash;references:ProfilePhotoHash;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
}

// v1

//type Account struct {
//	AccountId int64 `gorm:"primaryKey;AUTO_INCREMENT"`
//	UserId    int64
//	Balance   int64
//	DValue    int64
//	Created   int64          `gorm:"autoCreateTime:milli"`
//	Updated   int64          `gorm:"autoUpdateTime:milli"`
//	Deleted   gorm.DeletedAt `gorm:"index"`
//}

//type CashIn struct {
//	InId    int64 `gorm:"primaryKey;AUTO_INCREMENT"`
//	UserId  int64
//	Count   int64
//	Created int64 `gorm:"autoCreateTime:milli"`
//	Updated int64 `gorm:"autoUpdateTime:milli"`
//	State   string
//	Deleted gorm.DeletedAt `gorm:"index"`
//}

//type CashOut struct {
//	OutId   int64 `gorm:"primaryKey;AUTO_INCREMENT"`
//	UserId  int64
//	Count   int64
//	Created int64 `gorm:"autoCreateTime:milli"`
//	Updated int64 `gorm:"autoUpdateTime:milli"`
//	State   string
//	Deleted gorm.DeletedAt `gorm:"index"`
//}

// v2

type Account struct {
	AccountId int64 `gorm:"primaryKey;AUTO_INCREMENT"`
	UserId    int64 `gorm:"index"`
	Balance   int64
	DValue    int64
	Created   int64          `gorm:"autoCreateTime:milli"`
	Updated   int64          `gorm:"autoUpdateTime:milli"`
	Deal      Deal           `gorm:"foreignKey:DealId;references:AccountId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	Deleted   gorm.DeletedAt `gorm:"index"`
}

type CashIn struct {
	UserId  int64
	Count   int64
	Created int64 `gorm:"autoCreateTime:milli"`
	Updated int64 `gorm:"autoUpdateTime:milli"`
	State   string
	InId    string         `gorm:"primaryKey"`
	Deleted gorm.DeletedAt `gorm:"index"`
}

type CashOut struct {
	UserId  int64
	Count   int64
	Created int64 `gorm:"autoCreateTime:milli"`
	Updated int64 `gorm:"autoUpdateTime:milli"`
	State   string
	OutId   string         `gorm:"primaryKey"`
	Deleted gorm.DeletedAt `gorm:"index"`
}

type Good struct {
	GoodId     int64  `gorm:"primaryKey;AUTO_INCREMENT"`
	Created    int64  `gorm:"autoCreateTime:milli"`
	Updated    int64  `gorm:"autoUpdateTime:milli"`
	Name       string `gorm:"type:varchar(30);"`
	Des        string
	State      string
	Picture    string         `gorm:"type:char(128);"`
	Deleted    gorm.DeletedAt `gorm:"index"`
	OrderBuys  []OrderBuy     `gorm:"foreignKey:GoodId;references:GoodId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	OrderSales []OrderSale    `gorm:"foreignKey:GoodId;references:GoodId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
	Deals      []Deal         `gorm:"foreignKey:GoodId;references:GoodId;constraint:OnUpdate:CASCADE,OnDelete:SetNull;"`
}

type OrderBuy struct {
	BuyId    int64 `gorm:"primaryKey;AUTO_INCREMENT"`
	GoodId   int64
	BuyerId  int64
	MaxPrice int64
	Num      int64
	LeftNum  int64
	Exp      int64
	Created  int64 `gorm:"autoCreateTime:milli"`
	Updated  int64 `gorm:"autoUpdateTime:milli"`
	State    string
	Deleted  gorm.DeletedAt `gorm:"index"`
}

type OrderSale struct {
	SaleId   int64 `gorm:"primaryKey;AUTO_INCREMENT"`
	GoodId   int64
	SalerId  int64
	MinPrice int64
	Num      int64
	LeftNum  int64
	Exp      int64
	Created  int64 `gorm:"autoCreateTime:milli"`
	Updated  int64 `gorm:"autoUpdateTime:milli"`
	State    string
	Deleted  gorm.DeletedAt `gorm:"index"`
}

// v1

//type Deal struct {
//	DealId   int64 `gorm:"primaryKey;AUTO_INCREMENT"`
//	GoodId   int64
//	BuyerId  int64
//	SalerId  int64
//	Num      int64
//	DealCost int64
//	Exp      int64
//	Created  int64 `gorm:"autoCreateTime:milli"`
//	Updated  int64 `gorm:"autoUpdateTime:milli"`
//	State    string
//	Deleted  gorm.DeletedAt `gorm:"index"`
//}

// v2

type Deal struct {
	DealId    int64 `gorm:"primaryKey;AUTO_INCREMENT"`
	GoodId    int64
	BuyerId   int64
	SalerId   int64
	Num       int64
	DealCost  int64
	Exp       int64
	AccountId int64
	Created   int64 `gorm:"autoCreateTime:milli"`
	Updated   int64 `gorm:"autoUpdateTime:milli"`
	State     string
	Deleted   gorm.DeletedAt `gorm:"index"`
}

// 头像-用户量

type ProfilePhoto struct {
	ProfilePhotoHash string `gorm:"primaryKey;type:char(128);"`
	Links            int64
}

// 系统用户

type UserSys struct {
	UserSysId int64  `gorm:"primaryKey;AUTO_INCREMENT"`
	Nickname  string `gorm:"type:varchar(30);"`
	Email     string
	Password  string `gorm:"type:char(60);"`
}
